﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SL8.SL.Helpers;

namespace SL8.SL.Controls
{
    /// <summary>
    /// 时间返回搜索项
    /// </summary>
    [TemplatePart(Name = DateRangeSearchItem.PART_EndDatePicker, Type = typeof(DatePicker))]
    [TemplatePart(Name = DateRangeSearchItem.PART_StartDatePicker, Type = typeof(DatePicker))]
    public class DateRangeSearchItem : EditTextSearchItem
    {
        private const string PART_CategoryListBox = "CategoryListBox";
        private const string PART_StartDatePicker = "StartDatePicker";
        private const string PART_EndDatePicker = "EndDatePicker";

        /// <summary>
        /// 开始时间
        /// </summary>
        private DatePicker StartDatePicker
        {
            get;
            set;
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        private DatePicker EndDatePicker
        {
            get;
            set;
        }

        /// <summary>
        /// 显示类别的列表
        /// </summary>
        private ListBox CategoryListBox
        {
            get;
            set;
        }

        public DateRangeSearchItem()
        {
            this.DefaultStyleKey = typeof(DateRangeSearchItem);
        }

        public override void OnApplyTemplate()
        {
            this.UnsubscribeFromTemplatePartEvents();
            base.OnApplyTemplate();

            this.StartDatePicker = this.GetTemplateChild(DateRangeSearchItem.PART_StartDatePicker) as DatePicker;
            this.EndDatePicker = this.GetTemplateChild(DateRangeSearchItem.PART_EndDatePicker) as DatePicker;
            this.CategoryListBox = this.GetTemplateChild(DateRangeSearchItem.PART_CategoryListBox) as ListBox;

            this.SubscribeToTemplatePartEvents();
        }

        public override System.Linq.Expressions.LambdaExpression GetSearchExpression()
        {
            System.Linq.Expressions.LambdaExpression expression = null;

            if (this.StartDatePicker == null && this.EndDatePicker == null)
            {
                return null;
            }

            if (this.CategoryListBox.ItemsSource == null)
            {
                return null;
            }

            if (this.StartDatePicker.SelectedDate != null && this.EndDatePicker.SelectedDate != null)
            {
                DateTime startDate = this.StartDatePicker.SelectedDate.Value;
                DateTime endDate = this.EndDatePicker.SelectedDate.Value;

                if (startDate > endDate)
                {
                    startDate = this.EndDatePicker.SelectedDate.Value;
                    endDate = this.StartDatePicker.SelectedDate.Value;
                }

                endDate = endDate.Add(new TimeSpan(23, 59, 59));

                foreach (var item in this.CategoryListBox.ItemsSource)
                {
                    if (item is SearchColumnInfo)
                    {
                        SearchColumnInfo searchColumnInfo = (SearchColumnInfo)item;

                        if (searchColumnInfo.IsChecked)
                        {
                            System.Linq.Expressions.LambdaExpression startExpressionTemp = null;
                            System.Linq.Expressions.LambdaExpression endExpressionTemp = null;
                            System.Linq.Expressions.LambdaExpression expressionTemp = null;

                            startExpressionTemp = LinqExpressionHelper.GetGreaterThanOrEqualExpressionByEntityType(this.ParameterExpression,
                                        searchColumnInfo.ColumnName,
                                        startDate,
                                        this.EntityType);

                            endExpressionTemp = LinqExpressionHelper.GetLessThanOrEqualExpressionByEntityType(this.ParameterExpression,
                                        searchColumnInfo.ColumnName,
                                        endDate,
                                        this.EntityType);

                            expressionTemp = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.AndAlso(startExpressionTemp.Body, endExpressionTemp.Body), startExpressionTemp.Parameters);

                            if (expressionTemp != null)
                            {
                                if (expression == null)
                                {
                                    expression = expressionTemp;
                                }
                                else
                                {
                                    expression = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.OrElse(expression.Body, expressionTemp.Body), expression.Parameters);
                                }
                            }
                        }
                    }
                }

                return expression;
            }
            else if (this.StartDatePicker.SelectedDate != null)
            {
                // 只选开始时间，则认为搜索大于开始时间的数据
                foreach (var item in this.CategoryListBox.ItemsSource)
                {
                    if (item is SearchColumnInfo)
                    {
                        SearchColumnInfo searchColumnInfo = (SearchColumnInfo)item;

                        if (searchColumnInfo.IsChecked)
                        {
                            System.Linq.Expressions.LambdaExpression expressionTemp = null;

                            expressionTemp = LinqExpressionHelper.GetGreaterThanOrEqualExpressionByEntityType(this.ParameterExpression,
                                        searchColumnInfo.ColumnName,
                                        this.StartDatePicker.SelectedDate.Value,
                                        this.EntityType);

                            if (expressionTemp != null)
                            {
                                if (expression == null)
                                {
                                    expression = expressionTemp;
                                }
                                else
                                {
                                    expression = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.OrElse(expression.Body, expressionTemp.Body), expression.Parameters);
                                }
                            }
                        }
                    }
                }

                return expression;
            }
            else if (this.EndDatePicker.SelectedDate != null)
            {
                // 只选结束时间，则认为搜索小于结束时间的数据
                foreach (var item in this.CategoryListBox.ItemsSource)
                {
                    if (item is SearchColumnInfo)
                    {
                        SearchColumnInfo searchColumnInfo = (SearchColumnInfo)item;

                        if (searchColumnInfo.IsChecked)
                        {
                            System.Linq.Expressions.LambdaExpression expressionTemp = null;

                            expressionTemp = LinqExpressionHelper.GetLessThanOrEqualExpressionByEntityType(this.ParameterExpression,
                                        searchColumnInfo.ColumnName,
                                        this.EndDatePicker.SelectedDate.Value.Add(new TimeSpan(23, 59, 59)),
                                        this.EntityType);

                            if (expressionTemp != null)
                            {
                                if (expression == null)
                                {
                                    expression = expressionTemp;
                                }
                                else
                                {
                                    expression = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.OrElse(expression.Body, expressionTemp.Body), expression.Parameters);
                                }
                            }
                        }
                    }
                }

                return expression;
            }

            return expression;
        }
    }
}
